package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {

    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprService empExprService;
    @Override
    public PageResult getPageResult(EmpQueryParam param) {
        Page<Emp> objectPage = Page.of(param.getPage(), param.getPageSize());
        objectPage.addOrder(OrderItem.asc("update_time"));
//        LambdaQueryWrapper<Emp> lambdaQueryWrapper=new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.like(param.getName() != null && !param.getName().isEmpty(),Emp::getName,param.getName()).
//                eq(param.getGender()!=null,Emp::getGender,param.getGender()).
//                between(param.getEnd()!=null&&param.getBegin()!=null,Emp::getEntryDate,param.getEnd(),param.getBegin());
//        objectPage=page(objectPage,lambdaQueryWrapper);

       objectPage = lambdaQuery().like(param.getName() != null && !param.getName().isEmpty(),Emp::getName,param.getName()).
                eq(param.getGender()!=null,Emp::getGender,param.getGender()).
                between(param.getEnd()!=null&&param.getBegin()!=null,Emp::getEntryDate,param.getEnd(),param.getBegin()).
                page(objectPage);


        List<Emp> emps= objectPage.getRecords();
        List<Integer> deptIds = emps.stream().map(emp -> emp.getDeptId()).collect(Collectors.toList());

        //  批量查询部门名称
        if(!deptIds.isEmpty()){
            List<Dept> depts = deptService.listByIds(deptIds);
            emps.forEach(emp -> depts.forEach(dept -> {
                if (emp.getDeptId()!=null&&emp.getDeptId().equals(dept.getId())){
                    emp.setDeptName(dept.getName());
                }
            }));
        }


        return new PageResult<>(objectPage.getTotal(),emps);
    }

    @Override
    public void saveEmp(Emp emp) {

//        增加员工
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        save(emp);
//        增加工作经历
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Emp getByIdEmp(Integer id) {
//        查询员工信息
        Emp emp = getById(id);
//        查询员工经历信息
        LambdaQueryWrapper<EmpExpr> lambdaQueryWrapper=new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(EmpExpr::getEmpId,id);
        List<EmpExpr> list = empExprService.list(lambdaQueryWrapper);
        if (!list().isEmpty()) {
            emp.setExprList(list);
        }



        return emp;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getUpdateEmp(Emp emp) {
//        修改员工信息
        updateById(emp);

//
        List<EmpExpr> exprList = emp.getExprList();

        if (!exprList.isEmpty()){
//            删除工作经历
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,emp.getId()));
//        新增工作经历
        exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
        }
    }

    @Override
    @Transactional
    public void removeEmp(List<Integer> ids) {
        removeBatchByIds(ids);

//        删除员工工作经历
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId,ids));
    }


}
